package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.JsonFormat;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameBackpack;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-07-20 11:25
 **/
@Component(PtoNum.USERIDTOWEAPONRY + "")
public class UserIdToWeaponry implements ReceiveMsg {
    private final Logger logger = LoggerFactory.getLogger(UserIdToWeaponry.class);
    @Autowired
    private UserServiceCache userServiceCache;

    @Override
    public void execute(Message msg) {
        GameBackpack.UserIdToWeaponry userIdToWeaponry = (GameBackpack.UserIdToWeaponry) msg;
        switch (userIdToWeaponry.getCode()) {
            case CommonBackpack.UPDATE_WEAPONRY_USAGE_USE:
                transfer(userIdToWeaponry, "使用装备情况更新");
                break;
            case CommonBackpack.UPDATE_WEAPONRY_USAGE_UNUSED:
                transfer(userIdToWeaponry, "脱下装备情况更新");
                break;
            case CommonBackpack.SELF_WEAPONRY_BROADCAST:
                transfer(userIdToWeaponry, "装备情况更新");
                break;
            default:
                logger.info("UserIdWeaponry中的code并没匹配成功！");
                break;
        }
    }

    private void transfer(GameBackpack.UserIdToWeaponry userIdToWeaponry, String content) {
        for (GameEntity.Weaponry weaponry : userIdToWeaponry.getWeaponryList()) {
            userServiceCache.getUserInMap().get(userIdToWeaponry.getUserId())
                    .getWeaponryMap().put(weaponry.getSelfWeaponryId(), weaponry);
            logger.info("{}: {}！{}", userIdToWeaponry.getRoleId(), content, JsonFormat.printToString(weaponry));
        }
    }
}
